package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.util.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/admin/product")
public class FileUploadController {

    @Value("${fileServer.url}")
    private String url;
    @PostMapping("/fileUpload")
    public Result fileUpload(MultipartFile file){
        //调用文件管理工具类的文件上传方法
        String path = FileUtil.fileUpload(file);
        //判断上传结果
        if (StringUtils.isEmpty(path)){
            return Result.fail();
        }
        //将请求地址与返回的文件路径拼接返回
        return Result.ok(url+path);
    }
}
